package com.ryskoo.catering.coupondetail.repository;

import com.ryskoo.catering.coupondetail.domain.CouponDetailPO;
import com.ryskoo.common.hclconstant.CouponStateEnum;
import com.ryskoo.common.hclconstant.CouponTypeEnum;
import com.ryskoo.framework.core.bean.QueryConditionEnum;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;
import com.ryskoo.framework.core.utils.DateFormatHelper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CouponDetailJPAImpl extends BasicJPAImpl<CouponDetailPO> implements ICouponDetailRepository {
    @Override
    public List<CouponDetailPO> getListByCustomerUuidAndCouponUuids(String customerUuid, List<String> couponUuids) {
        QueryMessageDTO qm = new QueryMessageDTO();
        qm.addCondition("customerUuid", QueryConditionEnum.EQ.name(), customerUuid);
        qm.addCondition("couponUuid", QueryConditionEnum.IN.name(), couponUuids);
        return this.queryByCondition(qm, false);
    }

    @Override
    public List<CouponDetailPO> getListByCustomerUuidAndCouponUuid(String customerUuid, String couponUuid) {
        QueryMessageDTO qm = new QueryMessageDTO();
        qm.addCondition("customerUuid", QueryConditionEnum.EQ.name(), customerUuid);
        qm.addCondition("couponUuid", QueryConditionEnum.EQ.name(), couponUuid);
        return this.queryByCondition(qm, false);
    }

    @Override
    public List<CouponDetailPO> getCouponDetailList(String customerUuid, String productUuid, double price) {
        String nowTime = DateFormatHelper.getNowTimeStr();
        QueryMessageDTO qm = new QueryMessageDTO();
        qm.addCondition("customerUuid", QueryConditionEnum.EQ.name(), customerUuid);
        qm.addCondition("productUuid", QueryConditionEnum.EQ.name(), productUuid);
        qm.addCondition("couponType", QueryConditionEnum.EQ.name(), CouponTypeEnum.PRODUCT.getValue());
        qm.addCondition("state", QueryConditionEnum.EQ.name(), CouponStateEnum.UNUSED.getValue());
        qm.addCondition("startValidTime", QueryConditionEnum.LT.name(), nowTime);
        qm.addCondition("endValidTime", QueryConditionEnum.GE.name(), nowTime);
        qm.addCondition("reachPrice", QueryConditionEnum.LT.name(), price);
        return this.queryByCondition(qm, false);
    }

    @Override
    public List<CouponDetailPO> getVendorCouponDetailList(String customerUuid, String vendorUuid, double price) {
        String nowTime = DateFormatHelper.getNowTimeStr();
        QueryMessageDTO qm = new QueryMessageDTO();
        qm.addCondition("customerUuid", QueryConditionEnum.EQ.name(), customerUuid);
        qm.addCondition("vendorUuid", QueryConditionEnum.EQ.name(), vendorUuid);
        qm.addCondition("couponType", QueryConditionEnum.EQ.name(), CouponTypeEnum.VENDOR.getValue());
        qm.addCondition("state", QueryConditionEnum.EQ.name(), CouponStateEnum.UNUSED.getValue());
        qm.addCondition("startValidTime", QueryConditionEnum.LT.name(), nowTime);
        qm.addCondition("endValidTime", QueryConditionEnum.GE.name(), nowTime);
        qm.addCondition("reachPrice", QueryConditionEnum.LT.name(), price);
        return this.queryByCondition(qm, false);
    }
}
